package javaClassExercise.designPatterns.singleton;

/**
 * Created by lomo on 2017/3/26.
 *
 * 对Singleton类升级 -> 一个完整的单例类
 *
 */
public class CompleteSingleton {
    /* 定义私有类型的构造函数(默认为public级别)， 可以防止被实例化new*/
    private CompleteSingleton(){}

    /* 使用一个内部类来专门维护单例类 */
    private static class SingletonFactory{
        private static final CompleteSingleton instance = new CompleteSingleton();
    }

    /* 获取单例类的实例 */
    public static final CompleteSingleton getInstance() {
        return SingletonFactory.instance;
    }

    /* 如果该对象被用于序列化，可以保证对象在序列化前后保持一致 */
    /* 序列化：就是讲Java对象转换为字节序列的过程 */
    /* 序列化作用：将Java对象序列化成字节 然后保存于磁盘 或用于在网络传送字节 */
    public Object readResove() {
        return getInstance();
    }

    public static void main(String[] args) {
        CompleteSingleton completeSingleton = new CompleteSingleton();
        System.out.println(completeSingleton.getClass());
        //class javaClassExercise.designPatterns.singleton.CompleteSingleton

        CompleteSingleton.SingletonFactory t = new CompleteSingleton.SingletonFactory();
        System.out.println(t.getClass());
        //class javaClassExercise.designPatterns.singleton.CompleteSingleton$SingletonFactory

        System.out.println(CompleteSingleton.SingletonFactory.instance.getClass());
        //class javaClassExercise.designPatterns.singleton.CompleteSingleton
    }
}
